<!doctype html>
<html>
	<head>
		<meta charset="utf-8">
		<title>华为手机列表</title>
		<link href="css/list.css" type="text/css" rel="stylesheet">
	</head>
	<body>
		<?php require("/include/menu.php");?>
			<div class="container">
				<!--面包屑-->
				
				<?php
					include("../public/config.php");		//导入配置文件并连接数据库
					$link = @mysqli_connect(HOST,USER,PASS,DBNAME) or die("数据连接失败！");

					mysqli_set_charset($link,"utf8");		//设置编码
					//=====面包屑导航==========================================
					if(!empty($_GET['tid'])){
						$indexTip = " where id=".$_GET['tid'];
					}


					$sql = "select name from type ".$indexTip;		//设置sql语句
					// echo $sql;
					$result = mysqli_query($link,$sql);				//执行sql

					$row = mysqli_fetch_assoc($result);				//取出结果集
					echo '<div class="Crumbs">';
					echo '<a href="index.php">首页</a> >&nbsp;'.$row['name'].'';
					echo '</div>';									//输出面包屑
					//<!--图片展示-->


					//=====产品列表===========================================
					echo '<div class="phone_pic_list">				
							<ul>';

					//======搜索===========
					$wherelist = array();
					$urllist = array();

					if (!empty($_GET['search'])){				//接收用户的搜索数据
						$wherelist[] = "goods like '%{$_GET['search']}%'";
						$urllist[] = "goods={$_GET[search]}";
					}

					if (!empty($_GET['goods'])){				//接收用户的搜索数据
						$wherelist[] = "goods like '%{$_GET['goods']}%'";
						$urllist[] = "goods={$_GET[goods]}";
					}




					if(!empty($_GET['tid'])){
						$wherelist[] = " concat(path,type.id,',') like '%,{$_GET['tid']},%'";
						$urllist[] = "tid={$_GET[tid]}";
					}

					if (count($wherelist)>0) {					//判断是否有搜索数据
						$where =" and ".implode("and", $wherelist);
						$url = "&".implode("&", $urllist);
					}


					//=====分页==============
					$page = isset($_GET['p'])?$_GET['p']:1;//当前页
					$pageSize = 10;//页大小
					$maxRows = 0;//总条数
					$maxPage = 0;//总页数
														
					$sql = "select * from goods,type where goods.typeid=type.id ".$where;
					// echo $sql;								//拼接sql语句
					$result = mysqli_query($link,$sql);		
					
					$maxRows = mysqli_num_rows($result);					//取出数据的数量
					// echo $maxRows;

					$maxPage = ceil($maxRows/$pageSize);	//计算总页数
					// echo $maxPage;
					if($page>$maxPage){				//判断页数是否越界
						$page=$maxPage;
					}
					if($page<1){
						$page=1;
					}

					$limit = " limit ".(($page-1)*$pageSize).",".$pageSize;		//设置分页sql语句	


					$sql = "select goods.*,name from goods,type where goods.typeid=type.id  {$where} order by addtime desc ".$limit;//拼接sql语句
					 // echo "$sql";
					$result = mysqli_query($link,$sql);

					while($row = mysqli_fetch_assoc($result)){			//取出结果集的数据并输出
						$sql2 = "select count(*) num from comment where goodsid='{$row['id']}'";
						$res = mysqli_query($link,$sql2);
						$gd = mysqli_fetch_assoc($res);
						$count = $gd['num'];
						// echo $sql2;
						echo '<li>
								<a href="details.php?goods='.$row[goods].'&goodsid='.$row[id].'"><img src="../public/uploads/'.$row[picname].'"></a>
								<p><a href="#">'.$row[goods].'
									<i><strong>('.$row[descr].')</strong></i></a>
								</p>
								<h4>'.$row[price].'</h4>
								<div class="phone_pic_div_left">
									<a href="#">选购</a>
								</div>
								<div class="phone_pic_div_right">
									'.$count.'人评价
								</div>
							</li>';	
						}
					echo   '</ul>
						  </div>';
				?><br><br>
				<?php
					//输出页码信息
				echo '<div class="clear"></div>';
				echo '<div style="float:right;">';
				echo "当前第{$page}/{$maxPage}页 共计{$maxRows}条"; 
				echo "<a href='list.php?p=1{$url}'>首页</a>";
				echo "<a href='list.php?p=".($page-1)."{$url}'>上一页</a>";
				echo "<a href='list.php?p=".($page+1)."{$url}'>下一页</a>";
				echo "<a href='list.php?p={$maxPage}{$url}'>末页</a>";
				echo "<br><br>";
				echo "第";
				for($i=1;$i<=$maxPage;$i++){
					echo " <a href='list.php?p={$i}{$url}'>{$i}</a> ";
				}
				echo "页";
				echo '</div>';
				?>


				<!--图片展示结束-->
				<div class="clear"></div>
				<?php include("./include/webbottom.php")?>
	</body>
</html>